import json

import pandas as pd
import requests

df = pd.read_excel("C:\\Users\\user\\Desktop\\招投标问题模版2.2.xlsx")
data_list = df.values.tolist()


def call_api(question, chapter):
    question = "请用原文回答" + question
    url = "http://192.168.80.35:8092/qwen2/api"
    # url = "http://192.168.80.51:8088/economic/policy/ask"
    json_data = json.dumps({'question': question, 'chapter': chapter})
    response = requests.post(url, data=json_data)
    # print(response.text)  # 输出响应内容
    return response.text  # 输出响应内容


"""
for mylist in data_list:
    content = mylist[5]
    content = content.replace("\\n\\n", "\n")
    mylist[5] = content

df_from_list = pd.DataFrame(data_list)
df_from_list.to_excel(f'C:\\Users\\user\\Desktop\\zxc.xlsx', index=False)

"""

"""
n = 0;
fileName = "医科大外科"
fileName = "福建省级机关医院保安服务项目"
fileName = "四项能力建设"
for mylist in data_list:
    question = mylist[0]
    if (question != None):
        if len(f"{question}") > 5:
            if len(f"{mylist[2]}") < 5:
                n = n + 1

                chapter =f"{fileName}{int(mylist[1])}"
                print(f"=========={question}==={chapter}=============")
                result = call_api(question, chapter)
                data = json.loads(result)
                answer = data["answer"]
                answer = answer.replace("\\n\\n", "\n")
                mylist[2] = answer
                mylist[1] = chapter
                print(answer)
                print()
                if n % 5 == 0:
                    df_from_list = pd.DataFrame(data_list)
                    df_from_list.to_excel(f'C:\\Users\\user\\Desktop\\{fileName}.xlsx', index=False)

df_from_list = pd.DataFrame(data_list)
df_from_list.to_excel(f'C:\\Users\\user\\Desktop\\{fileName}.xlsx', index=False)
"""

"""


def textwrite(chapter,answer):
    s = datetime.now().strftime("%Y%m%d%H%M%S")
    path = f"C:\\Users\\user\\Desktop\\测试920\\技术和服务要求\\{chapter}-{s}.txt"
    wf = open(path, "w", encoding="UTF-8")
    wf.write(answer)
    wf.flush()
    wf.close()


def test(question,chapter):
    result = call_api(question, chapter)
    data = json.loads(result)
    answer = data["answer"]
    textwrite(chapter,answer)
    print(f"========={question}=========")
    print(answer)


chapter = "医科大外科5"
chapter2 = "四项能力建设5"
chapter3 = "福建省级机关医院保安服务项目5"

question = "技术和服务要求这一部分的内容，如果出现相同的内容，必须单独列出来，如果有配置清单也要回答"

test(question,chapter)
test(question,chapter2)
test(question,chapter3)
"""

# sql
sql = "insert into tender_question_template(`inspect_item`,`invite_search_content`,`invite_question_name`,`submit_search_content`,`submit_question_name`,`question_type`,`sort`,`inspect_item_type`) values('{v1}','{v2}','{v3}','{v4}','{v5}','{v6}','{v7}','{v8}');"
#sql = "insert into tender_question_template(`inspect_item`,`invite_question_name`,`submit_question_name`,`question_type`) values('{v1}','{v2}','{v3}','{v4}');"
for mylist in data_list:
    # chapter = int(mylist[1])
    # print(question," ",chapter)
    if len(mylist[8]) > 2:
        continue
    test = sql.format(v1=mylist[1], v2=mylist[2], v3=mylist[3], v4=mylist[4],v5=mylist[5],v6=mylist[6],v7=mylist[7],v8=mylist[9])
    #test = sql.format(v1=mylist[0], v2=mylist[1], v3=mylist[2], v4=mylist[3])
    print(test)
